#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>


int main()
{
    pid_t pid;
    if ( (pid=fork()) == -1 )
    {
        perror("forkerrno"),exit(1);
    }
    else if(pid==0)//son fork
    {
        printf("I am child process,sleeping 20s,My id is [%d]\n",getpid());
        sleep(5);
        exit(66);
    }
    else
    {
        int status=0;
        pid_t ret=0;
        while((ret=waitpid(-1,&status,WNOHANG))==0)//-1 any son ,0 blocked
        {
            sleep(1);

            printf("we are waiting son fork,son is running \n");
        }
        if(WIFEXITED(status)&&ret==pid)//check exit right or wrong
        {
            printf("wait 5s sussessful,child return [%d],child id is[%d]\n",WEXITSTATUS(status),pid);
        }
        else
        {
            printf("wait failed,return  \n");
        }
    }
    return 0;
}
